package com.zmn.plat.business.impl.sendtemplate;

import java.util.Objects;

import com.alibaba.fastjson.JSON;
import com.zmn.common.dto.PushMsgDTO;
import com.zmn.common.dto.ResultDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.manager.mq.utils.MQPushUtil;
import com.zmn.plat.business.interfaces.sendtemplate.SendSmsInfoBService;
import com.zmn.plat.common.dictionary.PlatErrorCodeDict;
import com.zmn.plat.common.exception.PlatException;
import com.zmn.plat.manager.constant.ZmnSmsConsts;
import com.zmn.plat.model.entity.message.MsgTemplate;
import com.zmn.plat.model.entity.push.ZmnSms;

import lombok.extern.slf4j.Slf4j;

/**
 * 描述：通过阿里短信发送消息 短信
 *
 * @author wangjie
 * @since 2020/6/19 10:42
 **/
@Slf4j
public class AliSendSmsInfoBServiceImpl implements SendSmsInfoBService {

    private static final String TAG = "通过阿里短信发送消息";

    private volatile static AliSendSmsInfoBServiceImpl aliSendSmsInfoBService;

    private AliSendSmsInfoBServiceImpl() {}

    @Override
    public Integer doSend(MsgTemplate msgTemplate, ZmnSms zmnSms) {

        // 验证必要条件
        if (StringUtil.isBlank(msgTemplate.getMessage())) {
            log.error("[{}],发送失败，短信内容不能为null。 code:{},msg:{}", TAG, PlatErrorCodeDict.ERROR_CODE_PUSH_MSG_AL_SMS_CONTENT_NONE, ZmnSmsConsts.MSG_NO_TEMPLATE_MESSAGE_ERROR_STR);
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PUSH_MSG_AL_SMS_CONTENT_NONE);
        }
        if (StringUtil.isBlank(zmnSms.getMobile())) {
            log.error("[{}],发送失败，接受手机号不能为null。 code:{},msg:{}", TAG, PlatErrorCodeDict.ERROR_CODE_PUSH_MSG_AL_SMS_MOBILE_NONE, ZmnSmsConsts.MSG_NO_TEMPLATE_MESSAGE_ERROR_STR);
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PUSH_MSG_AL_SMS_MOBILE_NONE);
        }

        String day = DateUtil.getNowFormatted(DateUtil.FORMAT_DATE);
        String id = zmnSms.getMobile() + "," + msgTemplate.getMessageMark();
        PushMsgDTO sms = new PushMsgDTO(id, msgTemplate.getWayId(), JSON.toJSONString(zmnSms.getJsonObject()));
        String key = zmnSms.getMobile() + "_" + day;
        ResultDTO result = MQPushUtil.sendAliSmsInfo(key, sms);
        log.info("[{}],发送参数。 key:{},msg:{}", TAG, key, sms);

        if (result.getStatusBool()) {
            log.info("[{}],发送成功。 code:{},msg:{}", TAG, ZmnSmsConsts.MSG_SEND_SUCCESS, ZmnSmsConsts.MSG_SEND_SUCCESS_STR);
            return ZmnSmsConsts.MSG_SEND_SUCCESS;
        } else {
            log.info("[{}],发送失败。 code:{},msg:{}", TAG, PlatErrorCodeDict.ERROR_CODE_PUSH_MSG_SEND_AL_ERROR, result.getMessage());
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PUSH_MSG_SEND_AL_ERROR);
        }
    }

    /**
     * 描述：获取该类的单例对象
     *
     * @return com.zmn.plat.business.impl.sendtemplate.ZmnAliSendSmsInfoBServiceImpl
     * @author wangjie
     * @since 2020/6/19 10:45
     **/
    public static AliSendSmsInfoBServiceImpl getSingleton() {
        if (Objects.isNull(aliSendSmsInfoBService)) {
            synchronized (AliSendSmsInfoBServiceImpl.class) {
                if (Objects.isNull(aliSendSmsInfoBService)) {
                    aliSendSmsInfoBService = new AliSendSmsInfoBServiceImpl();
                }
            }
        }
        return aliSendSmsInfoBService;
    }
}
